﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.CommonDelegate;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.Common;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers
{
    public class CommonController : Controller
    {
        //
        CommonDelegate  CommonDelegate = new CommonDelegate();

        public JsonResult GetDisplayNameByUserIdAjax(string userId)
        {
            try
            {
                string DisplayName = CommonDelegate.GetDisplayNameByUserId(userId);

                return Json(DisplayName, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(string.Empty, JsonRequestBehavior.AllowGet);
            }

        }

        public JsonResult GetDisplayNameListByUserIdListAjax(string userIdList)
        {
            try
            {
                IEnumerable<string> userIds = userIdList.Split('|');
                string DisplayNameList = string.Empty;
                TB_User user = new TB_User();

                for (int i = 0; i < userIds.Count() - 1; i++)
                {
                    user = CommonDelegate.GetUserNameByUserId(userIds.ElementAt(i));
                    if (i != userIds.Count() - 2)
                    {
                        DisplayNameList += (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.FullName)) ? user.UserName + "; " : user.FullName + "; ";
                    }
                    else
                    {
                        DisplayNameList += (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.FullName)) ? user.UserName : user.FullName;
                    }

                }



                return Json(DisplayNameList, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(string.Empty, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetDisplayNemeUserAndDepByUserId(int userID)
        {
            string kq = CommonDelegate.GetDisplayNameAndDepNameByUserId(userID.ToString());
            return Json(kq, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDisplayNemeUserAndDepByListUserId(string ListUserID)
        {
            string[] arr = ListUserID.Split(',');
            string listName = string.Empty;
            for (int i = 0; i < arr.Length; i++)
            {
                if (!string.IsNullOrEmpty(arr[i]))
                {
                    listName += CommonDelegate.GetDisplayNameAndDepNameByUserId(arr[i]) + ", ";
                }
            }
            if (!string.IsNullOrEmpty(listName))
                listName = listName.Substring(0, listName.Length - 2);
            return Json(listName, JsonRequestBehavior.AllowGet);
        }
    }
}
